﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThanhTu.GNCMedia.KQXS.DataAccess.DAL;
using ThanhTu.GNCMedia.KQXS.DataAccess.DO;
using System.Data;
using System.Collections;

namespace WebUI.Admin.UserControls
{
    public partial class ActionSyntax : System.Web.UI.UserControl
    {
        public int UserListCount { get; set; }
        int rowCount = 0;
        int pageIndex =1;
        int pageSize=5;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ProvinceDAL province = new ProvinceDAL();
                List<ProvinceDO> alldata = province.selectadd();

                ddl_IDtinh_Syntax.DataSource = alldata;
                ddl_IDtinh_Syntax.DataTextField = "princeName";
                ddl_IDtinh_Syntax.DataValueField = "provinceID";
                ddl_IDtinh_Syntax.DataBind();
                MessengerSyntaxDAL messengerSyntaxDAL = new MessengerSyntaxDAL();
                DataTable dt = messengerSyntaxDAL.SelectAll_Messenger(pageIndex,pageSize, ref rowCount);
                RepeaterSyntax.DataSource = dt;
                RepeaterSyntax.DataBind();
                for (int i = 1; i <=rowCount/pageSize; i++)
                {
                    ddPage.Items.Add(i.ToString());
                }
            }
        }

        protected void btn_addNewData_Click1(object sender, EventArgs e)
        {

            MessengerSyntaxDAL messengerSyntax = new MessengerSyntaxDAL();
            MessengerSyntaxDO messengerDO = new MessengerSyntaxDO();
            messengerDO.ProvinceID = ddl_IDtinh_Syntax.SelectedValue;
            messengerDO.Syntax = txt_Syntax.Text;
            messengerDO.SwitchBoard = txt_tongDai_Syntax.Text;
           int checkInsert= messengerSyntax.add(messengerDO);
           if (checkInsert > 0)
           {
               Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowStatus", "javascript:alert('Thêm Mới CÚ PHÁP Tin Nhắn Thành công);", true);
           }
           else
           {
               Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowStatus", "javascript:alert('LỖI THÊM MỚI CÚ PHÁP ,XIN VUI LÒNG THỬ LẠI);", true);
           }
        }

        protected void RepeaterSyntax_mmand(object source, RepeaterCommandEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                MessengerSyntaxDAL messengerSyntax = new MessengerSyntaxDAL();
               int checkDel= messengerSyntax.delete(e.CommandName);
               if (checkDel > 0)
               {
                   Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowStatus", "javascript:alert('XÓA CÚ PHÁP Tin Nhắn Thành công);", true);
               }
               else
               {
                   Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowStatus", "javascript:alert('LỖICXÓA CÚ PHÁP ,XIN VUI LÒNG THỬ LẠI);", true);
               }
            }

        }

        protected void lbtntnext_Click(object sender, EventArgs e)
        {
            pageSize += 5;
            MessengerSyntaxDAL messengerSyntaxDAL = new MessengerSyntaxDAL();
            DataTable dt = messengerSyntaxDAL.SelectAll_Messenger(pageIndex++, pageSize, ref rowCount);
            RepeaterSyntax.DataSource = dt;
            RepeaterSyntax.DataBind();
        }

        protected void lbtn_first_Click(object sender, EventArgs e)
        {
            MessengerSyntaxDAL messengerSyntaxDAL = new MessengerSyntaxDAL();
            DataTable dt = messengerSyntaxDAL.SelectAll_Messenger(pageIndex, pageSize, ref rowCount);
            RepeaterSyntax.DataSource = dt;
            RepeaterSyntax.DataBind();
        }

        protected void lbtn_pre_Click(object sender, EventArgs e)
        {
            MessengerSyntaxDAL messengerSyntaxDAL = new MessengerSyntaxDAL();
            DataTable dt = messengerSyntaxDAL.SelectAll_Messenger(pageIndex, pageSize, ref rowCount);
            int totalrow = rowCount;
            pageIndex = totalrow / pageSize;

            if (pageIndex > 1)
            {
                DataTable dt1 = messengerSyntaxDAL.SelectAll_Messenger(pageIndex--, pageSize, ref rowCount);
                RepeaterSyntax.DataSource = dt;
                RepeaterSyntax.DataBind();
            }
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
           
            MessengerSyntaxDAL messengerSyntaxDAL = new MessengerSyntaxDAL();
            DataTable dt = messengerSyntaxDAL.SelectAll_Messenger(pageIndex, pageSize, ref rowCount);
            RepeaterSyntax.DataSource = dt;
            RepeaterSyntax.DataBind();
            int lastpage = rowCount;
            DataTable dt1 = messengerSyntaxDAL.SelectAll_Messenger(pageIndex,lastpage , ref rowCount);
            RepeaterSyntax.DataSource = dt1;
            RepeaterSyntax.DataBind();
        }

        protected void ddPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            int runToPage = Convert.ToInt32(ddPage.SelectedItem.Text);
            MessengerSyntaxDAL messengerSyntaxDAL = new MessengerSyntaxDAL();
            DataTable dt = messengerSyntaxDAL.SelectAll_Messenger(pageIndex, pageSize, ref rowCount);
            RepeaterSyntax.DataSource = dt;
            RepeaterSyntax.DataBind();
            if (runToPage == rowCount / runToPage)
            {
                DataTable dt2 = messengerSyntaxDAL.SelectAll_Messenger(pageIndex, rowCount, ref rowCount);
                RepeaterSyntax.DataSource = dt2;
                RepeaterSyntax.DataBind();
            }
            else
            {
                if (runToPage == 1)
                {
                    DataTable dt3 = messengerSyntaxDAL.SelectAll_Messenger(pageIndex, pageSize, ref rowCount);
                    RepeaterSyntax.DataSource = dt3;
                    RepeaterSyntax.DataBind();
                }
                else
                {
                    int dump = rowCount / runToPage;
                    DataTable dt1 = messengerSyntaxDAL.SelectAll_Messenger(pageIndex, dump, ref rowCount);
                    RepeaterSyntax.DataSource = dt1;
                    RepeaterSyntax.DataBind();
                }
            }
        }
    }
}